package cn.smbms.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.smbms.dao.BaseDao;
import cn.smbms.dao.BillMapper;
import cn.smbms.dao.ProviderMapper;
import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.service.ProviderService;

@Service("providerService")
public class ProviderServiceImpl implements ProviderService {
	@Autowired(required = false)
	private ProviderMapper provderMapper;
	@Autowired(required = false)
	private BillMapper billMapper;

	@Override
	public boolean add(Provider provider) {
		// TODO Auto-generated method stub
		boolean flag = false;
		Connection connection = null;
		try {
			connection = BaseDao.getConnection();
			connection.setAutoCommit(false);// 开启JDBC事务管理
			if (provderMapper.add(provider) > 0)
				flag = true;
			connection.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				System.out.println("rollback==================");
				connection.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} finally {
			// 在service层进行connection连接的关闭
			BaseDao.closeResource(connection, null, null);
		}
		return flag;
	}

	@Override
	/**
	 * 通过供应商名称、编码获取供应商列表-模糊查询-providerList
	 * 
	 * @param connection
	 * @param proName
	 * @return
	 * @throws Exception
	 */
	public List<Provider> getProviderList(@Param("proName") String proName, @Param("proCode") String proCode,
			@Param("from") Integer currentPageNo, @Param("pageSize") Integer pageSize) throws Exception {

		// TODO Auto-generated method stub
		//Connection connection = null;
		List<Provider> providerList = null;
		System.out.println("query proName ---- > " + proName);
		System.out.println("query proCode ---- > " + proCode);
		System.out.println("query currentPageNo ---- > " + currentPageNo);
		System.out.println("query pageSize ---- > " + pageSize);
		try {
			//connection = BaseDao.getConnection();
			currentPageNo = (currentPageNo - 1) * pageSize;
			System.out.println(currentPageNo+"currentPageNo Service"+pageSize+"pageSize");
			providerList = provderMapper.getProviderList(proName, proCode, currentPageNo, pageSize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			//.closeResource(connection, null, null);
		}
		return providerList;
	}

	@Override
	public int getProviderCount(String proName, String proCode) {
		Connection connection = null;
		int count = 0;
		System.out.println("productName ---- > " + proName);
		System.out.println("providerId ---- > " + proCode);
		try {
			connection = BaseDao.getConnection();
			count = provderMapper.getProviderCount(proName, proCode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeResource(connection, null, null);
		}
		return count;

	}

	/**
	 * 业务：根据ID删除供应商表的数据之前，需要先去订单表里进行查询操作 若订单表中无该供应商的订单数据，则可以删除 若有该供应商的订单数据，则不可以删除
	 * 返回值billCount 1> billCount == 0 删除---1 成功 （0） 2 不成功 （-1） 2> billCount > 0 不能删除
	 * 查询成功（0）查询不成功（-1）
	 * 
	 * ---判断 如果billCount = -1 失败 若billCount >= 0 成功
	 */
	@Override
	public int deleteProviderById(String delId) {
		// TODO Auto-generated method stub
		Connection connection = null;
		int billCount = -1;
		try {
			connection = BaseDao.getConnection();
			connection.setAutoCommit(false);
			billCount = billMapper.getBillCountByProviderId(delId);
			if (billCount == 0) {
				provderMapper.deleteProviderById(delId);
			}
			connection.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			billCount = -1;
			try {
				connection.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} finally {
			BaseDao.closeResource(connection, null, null);
		}
		return billCount;
	}

	@Override
	public List<Provider> getProviderById() {
		// TODO Auto-generated method stub
		List<Provider> provider = null;
		Connection connection = null;
		try {
			connection = BaseDao.getConnection();
			provider = provderMapper.getProviderById();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			provider = null;
		} finally {
			BaseDao.closeResource(connection, null, null);
		}
		return provider;
	}

	@Override
	public boolean modify(Provider provider) {
		// TODO Auto-generated method stub
		Connection connection = null;
		boolean flag = false;
		try {
			connection = BaseDao.getConnection();
			if (provderMapper.modify(provider) > 0)
				flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeResource(connection, null, null);
		}
		return flag;
	}

}
